package com.easylive.admin.controller;

import com.easylive.entity.po.CategoryInfo;
import com.easylive.entity.query.CategoryInfoQuery;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.service.CategoryInfoService;
import com.easylive.utils.StringTools;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController extends ABaseController{
    @Resource
    private CategoryInfoService categoryInfoService;

    @RequestMapping("/loadCategory")
    public ResponseVO loadDataList(CategoryInfoQuery query){
        query.setOrderBy("sort asc");
        query.setConvert2Tree(true);
        List<CategoryInfo> categoryInfoList = categoryInfoService.findListByParam(query);

        return getSuccessResponseVO(categoryInfoList);
    }

    @RequestMapping("/saveCategory")
    public ResponseVO save(@NotEmpty Integer pCategoryId,
                           Integer categoryId,
                           @NotEmpty String categoryCode,
                           @NotEmpty String categoryName,
                           String icon,
                           String background){
        CategoryInfo categoryInfo = new CategoryInfo();
        categoryInfo.setCategoryCode(categoryCode);
        categoryInfo.setpCategoryId(pCategoryId);
        categoryInfo.setCategoryName(categoryName);
        categoryInfo.setCategoryId(categoryId);
        categoryInfo.setIcon(icon);
        categoryInfo.setBackground(background);

        categoryInfoService.saveCategory(categoryInfo);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/delCategory")
    public ResponseVO save(@NotNull Integer categoryId){

        categoryInfoService.delCategory(categoryId);
        return getSuccessResponseVO(null);

    }

    @RequestMapping("/changeSort")
    public ResponseVO changeSort(@NotNull Integer pCategoryId,
                                 @NotEmpty String categoryIds){

        categoryInfoService.changeSort(pCategoryId, categoryIds);
        System.out.println(categoryIds);
        return getSuccessResponseVO(null);

    }



}
